<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkTest')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

/** 
 *  This is base class for unit testing when developing with PDK plugin using SimpleTest framework via SimpleTest plugin
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
 //NOTE: Althought we do this include here, you should also include lanceur_spip.php in your test case
include_spip('inc/tests');
include_spip('functions/pdk_common');

/**
 * PdkTest
 * 
 * @package spip_test
 * @author Aleks
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class PdkTest extends SpipTest {
    /**
     *  PdkTestTable
     */
    protected $test_table;
    
    /**
     *  Boolean value wich indicates whether to create test tables before testing 
     */
    protected $createTestTables = true;
    
    /**
     *  Boolean value wich indicates whether to repopulate (clear and populate) all tables with test data before each test 
     */
    protected $repopulateTestTables = true;
    
    /**
     *  Boolean value wich indicates whether to delete test tables after testing 
     */
    public $dropTestTables = true;
    
    /**
     *  Array of PDK models wich will be used in unit tests
     *  Add model to this array to automatically create, delete and clear it's tables
     *  Format: array([pdk_model_name1]=>PdkModel1,[pdk_model_name2]=>PdkModel2)
     */
    protected $pdk_models = array();
    
    public function __construct($name = false) {      
        $this->SpipTest($name);
        //include_spip('classes/PdkException');
        pdk_load_class('PdkException', 'classes');
        $this->test_table = pdk_load_class('PdkTestTable', 'classes/simpletest/');
    }    
    
    public function __destruct() { 
        if (!$this->dropTestTables) return;        
        foreach ($this->pdk_models as $pdk_model) {            
            $this->test_table->DropTables($pdk_model);
        }
    }
    
    private function createTables() {        
        if ($this->createTestTables)
            foreach ($this->pdk_models as $pdk_model) {            
                $this->test_table->CreateTables($pdk_model);
            }
    }
    
    /**
     *  Add PDK Model wich will be used while unit testing
     */
    public function AddPdkModel($name, pdk_Model $model) { 
        $this->AddPdkModels(array($name=>$model));        
    }
    /**
     *  Add array of PDK Models wich will be used while unit testing
     *  Test tables for this models will be created automatically
     */
    public function AddPdkModels($models) {
        if (!isset($models) || !is_array($models) || empty($models))
            throw new PdkException("Bad array of PDK Models given for AddPdkModels function");
        
        foreach ($models as $name=>$model) {
            if (isset($this->pdk_models[$name])) throw new PdkException($name." model has been already added to PdkTest pdk_models array");
            $this->pdk_models[$name] = $model;    
        }     
        
        $this->createTables();
    }
    
    /**
     *  Run before each test
     */
    function setUp() {        
        if ($this->repopulateTestTables) {
            //Clear and populate tables        
            foreach ($this->pdk_models as $model_name=>$pdk_model) {                
                if (!isset($pdk_model->test_data) || !is_array($pdk_model->test_data) || empty($pdk_model->test_data))
                    throw new PdkException("Test data array isn't provided for ".$model_name." model class");
                $this->test_table->ClearTables($pdk_model);
                $this->test_table->PopulateTables($pdk_model);                
            }            
        }
    }
}
?>